Fix handling of input of type FormData for the Fetch client #1008
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Several open issues are caused by the
multipart/form-data
formatter trying to provide a simple API based on an input of typeObject
. However,Object
can not express some of the typical use cases of a multipart request. On the other hand,FormData
could.One example (mentioned in #705 and #965) is sending a requested with multiple entries belonging to the same "key". Since a
FormData
object allows one key to be mapped to multiple values, one can simply do:This is clearly not possible with an
Object
, where the key-value relationship is 1:1. #1000 tries to achieve this by automatically convertingArray
values to multipleFormData
keys. However, I argue that it is not necessarily what the user wants, since they could either expect:FormData
entry, orFormData
entries.In principle, it is not so easy to infer which of the two is correct.
Currently, however, inputs of type
FormData
are not correctly handled by themultipart/form-data
formatter for the Fetch client, which tries to enumerate the keys using theObject.keys(...)
construct:swagger-typescript-api/templates/base/http-clients/fetch-http-client.ejs
Lines 107 to 108 in 325e308
Unfortunately,
FormData
returns an empty list when used throughObject.keys()
This PR proposes a way to allow the user to directly supply a
FormData
, basically opting out of any smart formatting logic that might actually get in the way. In this way, the user takes responsibility for correctly managing the conversion.I argue that the breaking change is minimal since any use of
FormData
is broken under the current API. Additionally, this would align with the logic for the Axios client, which already short-circuits the formatting formFormData
inputs.swagger-typescript-api/templates/base/http-clients/axios-http-client.ejs
Lines 81 to 82 in 325e308